{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import bisect\n",
    "import gc\n",
    "import os\n",
    "import pickle\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.compose import make_column_transformer\n",
    "from sklearn.impute import SimpleImputer\n",
    "from sklearn.pipeline import make_pipeline\n",
    "from sklearn.preprocessing import OrdinalEncoder, StandardScaler\n",
    "from utils.misc import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "DATA_DIR = os.path.abspath('../../Data/display_advertising_challenge/processed')\n",
    "USE_QUIZ_SET = False\n",
    "USE_TEST_SET = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "if USE_QUIZ_SET:\n",
    "    train_dataset_type = 'train+valid+test'\n",
    "    test_dataset_type = 'quiz'\n",
    "    \n",
    "elif USE_TEST_SET:\n",
    "    train_dataset_type = 'train+valid'\n",
    "    test_dataset_type = 'test'\n",
    "    \n",
    "else: \n",
    "    train_dataset_type = 'train'\n",
    "    test_dataset_type = 'valid'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_train = pd.read_pickle(os.path.join(DATA_DIR, '_'.join(['df', train_dataset_type]) + '.pkl'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "target_name = 'label'\n",
    "num_feature_names = df_train.columns[df_train.columns.str.startswith('I')]\n",
    "cat_feature_names = df_train.columns[df_train.columns.str.startswith('C')]\n",
    "all_feature_names = pd.Index(num_feature_names.to_list() + cat_feature_names.to_list())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# of num features: 13 \n",
      "# of cat features: 26\n"
     ]
    }
   ],
   "source": [
    "print('# of num features:', len(num_feature_names), '\\n# of cat features:', len(cat_feature_names))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "num_pipeline = make_pipeline(SimpleImputer(strategy='constant', fill_value=0.0, copy=False), \n",
    "                             StandardScaler(copy=False))\n",
    "cat_pipeline = make_pipeline(SimpleImputer(strategy='constant', fill_value='<unknown>', copy=False), \n",
    "                             OrdinalEncoder(dtype=np.int))\n",
    "full_pipeline = make_column_transformer((num_pipeline, num_feature_names), (cat_pipeline, cat_feature_names))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_y_train = df_train[target_name]\n",
    "df_y_train.to_pickle(os.path.join(DATA_DIR, '_'.join(['df', 'y', train_dataset_type]) + '.pkl'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "full_pipeline = full_pipeline.fit(df_train[all_feature_names])\n",
    "\n",
    "for i in range(len(full_pipeline.transformers_[1][2])):\n",
    "    categories = set(full_pipeline.transformers_[1][1].steps[1][1].categories_[i])\n",
    "    if '<unknown>' not in categories:\n",
    "        categories = list(categories)\n",
    "        bisect.insort_left(categories, '<unknown>')\n",
    "        full_pipeline.transformers_[1][1].steps[1][1].categories_[i] = np.array(categories)\n",
    "        \n",
    "n_categories = {feature: len(categories) for feature, categories in zip(\n",
    "    full_pipeline.transformers_[1][2], full_pipeline.transformers_[1][1].steps[1][1].categories_)}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Elapsed time: 3881 sec\n"
     ]
    }
   ],
   "source": [
    "with get_elapsed_time():\n",
    "    df_X_train = full_pipeline.transform(df_train[all_feature_names])\n",
    "    df_X_train = pd.DataFrame(df_X_train, columns=all_feature_names)\n",
    "    df_X_train = df_X_train.astype({feature_name: 'int' for feature_name in cat_feature_names}, copy=False)\n",
    "    df_X_train.to_pickle(os.path.join(DATA_DIR, '_'.join(['df', 'X', train_dataset_type]) + '.pkl'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "dump_pickle(os.path.join(DATA_DIR, '_'.join([train_dataset_type, 'pipeline.pkl'])), full_pipeline)\n",
    "dump_pickle(os.path.join(DATA_DIR, '_'.join([train_dataset_type, 'metadata.pkl'])), \n",
    "            (target_name, num_feature_names, cat_feature_names, n_categories))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "del df_train, df_y_train, df_X_train\n",
    "_ = gc.collect()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_test = pd.read_pickle(os.path.join(DATA_DIR, '_'.join(['df', test_dataset_type]) + '.pkl'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i, feature in enumerate(full_pipeline.transformers_[1][2]):\n",
    "    categories = set(full_pipeline.transformers_[1][1].steps[1][1].categories_[i])\n",
    "    df_test[feature] = df_test[feature].map(lambda x: np.nan if x not in categories else x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "if not USE_QUIZ_SET:\n",
    "    df_y_test = df_test[target_name]\n",
    "    df_y_test.to_pickle(os.path.join(DATA_DIR, '_'.join(['df', 'y', test_dataset_type]) + '.pkl'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Elapsed time: 114 sec\n"
     ]
    }
   ],
   "source": [
    "with get_elapsed_time():\n",
    "    df_X_test = full_pipeline.transform(df_test)\n",
    "    df_X_test = pd.DataFrame(df_X_test, columns=all_feature_names)\n",
    "    df_X_test = df_X_test.astype({feature_name: 'int' for feature_name in cat_feature_names}, copy=False)\n",
    "    df_X_test.to_pickle(os.path.join(DATA_DIR, '_'.join(['df', 'X', test_dataset_type]) + '.pkl'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "if not USE_QUIZ_SET:\n",
    "    del df_y_test, df_X_test\n",
    "    \n",
    "else:\n",
    "    del df_X_test\n",
    "_ = gc.collect()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
